<template>
	<view class="app">
		<view class="imgDiv">
			<!-- ?logoUrl:getUrl('/logo.png') -->
			<image :src="logoUrl" mode="heightFix"></image>
		</view>
		<button class="loginBtn" open-type="getPhoneNumber" withCredentials="true" @getphonenumber="getPhoneNumber">授权登录</button>
		<view class="phoneLogin" @click="toPhone">取消登录</view>
		<view class="agree">
			<u-checkbox-group>
				<u-checkbox v-model="checkbox" shape="circle" activeColor="#5EB847" :name="true">
				</u-checkbox>
			</u-checkbox-group>
			<view class="agree-text">
				我已阅读并同意
				<view class="agree-text-h" @click="toHtmlPage('用户协议')">《用户协议》</view>
				和
				<view class="agree-text-h" @click="toHtmlPage('隐私政策')">《隐私政策》</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { reactive, ref } from 'vue'
	import { onShow, onLoad } from "@dcloudio/uni-app"
	import { getUrl } from '@/utils/imagePlugin'
	import Api from '@/api/modules/home.ts'
	
	let checkbox = ref<boolean>(false)
	let sessionId = ref<any>('')
	
	let invitionCodeType = ref<any>('')
	let invitationCode = ref<any>('')
	let code = ref<any>('')
	let avatar = ref<any>('')
	let nickname = ref<any>('')
	let jscode = ref<string>('')
	// 查看已授权选项
	const getSettingMes = () => {
		uni.getSetting({
			success(res) {
				uni.login({
					provider: 'weixin',
					success: (codeRes) => {
						console.log("扣得", codeRes);
						if (codeRes.code) {
							jscode.value = codeRes.code
							// _this.$http.get(getSessionId + codeRes.code).then(sessionRes => {
							// 	if(sessionRes.code == 200){
							// 		_this.sessionId = sessionRes.data
							// 	} else {
							// 		console.log('授权失败！');
							// 	}
							// })
						} else {
							console.log('授权失败！' + res.errMsg);
						}
					}
				});
			},
			fail() {
				console.log('获取已授权选项失败');
			}
		});
	}
	
	const toPhone = () => {
		// uni.navigateTo({
		// 	url: '/pages/login/login'
		// })
		// Api.getInvitationCodeByUser()
		uni.navigateBack()
	}
	
	const getPhoneNumber = async(e: any) => {
		if (!checkbox.value) {
			uni.showToast({ title: '为了更好的保障您的权益，请您阅读并同意《用户协议》和《隐私政策》', icon: 'none' });
			return;
		}
		wx.getUserInfo({
			success (UserRes) {
				let params = {
					code: e.detail.code,
					jscode: jscode.value,
					encryptedData: UserRes.encryptedData,
					iv: UserRes.iv,
					invitationCode: invitationCode.value,
				}
				Api.wxLogin(params).then((res: any) => {
					uni.setStorageSync('token', res.data.token)
					uni.showToast({ title: '登录成功！', icon: 'none' });
					setTimeout(()=>{
						if(invitionCodeType.value == '1'){
							uni.redirectTo({
								url: `/pages/joinTeam/joinTeam?code=${
									code.value}&avatar=${
									avatar.value}&nickname=${
									nickname.value}`
							})
						} else {
							uni.switchTab({
								url: '/pages/home/home'
							})
						}
						
					}, 1500)
				}).catch((err: any) => {
					uni.showToast({ title: err.msg, icon: 'none' });
				})
			}
		});
	}
	let logoUrl = ref<string>('')
	const getLogo = () => {
		Api.getSystemInfo().then((res: any) => {
			logoUrl.value = res.data?.logoUrl
		})
	}
	
	const toHtmlPage = (type: string) => {
		uni.navigateTo({
			url: '/pages/document/document?type=' + type
		})
	}
	
	onLoad((options: any) => {
		if(options.invitionCodeType == '0'){
			invitationCode.value = options.code
			invitionCodeType.value = options.invitionCodeType
		} else if(options.invitionCodeType == '1'){
			if(uni.getStorageSync('token')){
				uni.redirectTo({
					url: `/pages/joinTeam/joinTeam?code=${
						options.code}&avatar=${
						options.avatar}&nickname=${
						decodeURIComponent(options.nickname)}`
				})
			} else {
				code.value = options.code
				avatar.value = options.avatar
				// nickname.value = options.nickname
				nickname.value = decodeURIComponent(options.nickname)
				invitionCodeType.value = options.invitionCodeType
			}
		}
		// const launchOptions = wx.getLaunchOptionsSync();
		// console.log('启动参数:', launchOptions);
		// console.log('启动参数:', options);
		// if (launchOptions.scene === 1044 || launchOptions.scene === 1048 || launchOptions.scene === 1047) {
		// 	// console.log(launchOptions.query.avatar)
		// 	console.log(launchOptions.query.code)
		// 	console.log(launchOptions.query.invitionCodeType)
		// 	console.log(decodeURIComponent(launchOptions.query.nickname))
		// 	if(launchOptions.query.invitionCodeType == '0'){
		// 		invitationCode.value = launchOptions.query.code
		// 		invitionCodeType.value = launchOptions.query.invitionCodeType
		// 	} else if(launchOptions.query.invitionCodeType == '1'){
		// 		if(uni.getStorageSync('token')){
		// 			uni.redirectTo({
		// 				url: `/pages/joinTeam/joinTeam?code=${
		// 					launchOptions.query.code}&avatar=${
		// 					launchOptions.query.avatar}&nickname=${
		// 					decodeURIComponent(launchOptions.query.nickname)}`
		// 			})
		// 		} else {
		// 			invitationCode.value = launchOptions.query.code
		// 			avatar.value = launchOptions.query.avatar
		// 			nickname.value = decodeURIComponent(launchOptions.query.nickname)
		// 			invitionCodeType.value = launchOptions.query.invitionCodeType
		// 		}
		// 	}
		// } else {
		// 	if(options.invitionCodeType == '0'){
		// 		invitationCode.value = options.code
		// 		invitionCodeType.value = options.invitionCodeType
		// 	} else if(options.invitionCodeType == '1'){
		// 		if(uni.getStorageSync('token')){
		// 			uni.redirectTo({
		// 				url: `/pages/joinTeam/joinTeam?code=${
		// 					options.code}&avatar=${
		// 					options.avatar}&nickname=${
		// 					decodeURIComponent(options.nickname)}`
		// 			})
		// 		} else {
		// 			invitationCode.value = options.code
		// 			avatar.value = options.avatar
		// 			// nickname.value = options.nickname
		// 			nickname.value = decodeURIComponent(options.nickname)
		// 			invitionCodeType.value = options.invitionCodeType
		// 		}
		// 	}
		// }
		
		getSettingMes()
		getLogo()
	})
	
</script>


<style scoped lang='scss'>
	.app {
		text-align: center;
		padding: 20px;
		background: #ffffff;
		height: 100vh;
	}

	.imgDiv {
		height: 200rpx;
		margin: 200rpx auto 300rpx;

		image {
			/* width: 100%; */
			height: 100%;
			/* border-radius: 50%; */
		}
	}
	
	.title{
		
		.titleText{
			font-size: 42rpx;
			font-weight: 600;
			margin-bottom: 30rpx;
		}
		
		.sub{
			color: #808080;
			font-weight: 600;
			font-size: 28rpx;
			margin-bottom: 80rpx;
		}
	}
	
	.loginBtn{
		height: 80rpx;
		line-height: 80rpx;
		font-size: 34rpx;
		border-radius: 80rpx;
		background: #5EB847;
		color: #ffffff;
		margin-bottom: 40rpx;
	}
	
	.phoneLogin{
		height: 80rpx;
		line-height: 80rpx;
		font-size: 34rpx;
		border-radius: 80rpx;
		text-align: center;
		color: #000000;
		background: #ffffff;
		border: 1rpx solid #5EB847;
	}
	
	.agree{
		display: flex;
		justify-content: center;
		margin: 40rpx 0;
		
		.agree-text{
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			color: #666;
			text-align: center;
			font-family: "PingFang SC";
			font-size: 28rpx;
			font-style: normal;
			font-weight: 400;
			line-height: normal;
			
			.agree-text-h{
				color: #5EB847;
				margin: 0 12rpx;
			}
		}
	}

	.mt20 {
		margin-top: 20px;
	}
</style>
